<template>
  <n-card :bordered="false" class="proCard">
    <BasicForm @register="register" @reset="handleReset" @submit="handleSubmit">
    </BasicForm>
    <BasicTable
      ref="actionRef"
      :actionColumn="actionColumn"
      :columns="columns"
      :request="loadDataTable"
      :row-key="(row) => row.id"
      :scroll-x="2048"
      @update:checked-row-keys="onCheckedRow"
    >
      <template #toolbar>
        <n-space>
          <n-button type="primary" @click="reloadTable">刷新数据</n-button>
        </n-space>
      </template>
    </BasicTable>
  </n-card>
</template>

<script lang="ts" setup>
  import { h, onMounted, reactive, ref, unref } from 'vue';
  import { useDialog, useMessage } from 'naive-ui';
  import { BasicTable, TableAction } from '@/components/Table';
  import { BasicForm, FormSchema, useForm } from '@/components/Form/index';
  import { columns } from './columns';
  import { useRouter } from 'vue-router';
  import { getPayConfigList, changeStatus } from '@/api/config/thirdConfig/alipayApi';
  import { formatToDate } from '@/utils/dateUtil';
  import { useUserStore } from '@/store/modules/user';

  const schemas: FormSchema[] = [
    {
      field: 'type',
      component: 'NSelect',
      label: '支付类型',
      defaultValue: '',
      componentProps: {
        options: (function () {
          var arr = [
          {
              label: '全部',
              value: '',
            },
            {
              label: 'APP',
              value: 'APP',
            },
            {
              label: 'H5',
              value: 'H5',
            },
            {
              label: 'Trans',
              value: 'Trans',
            },
          ];
          return arr;
        })(),
        onInput: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'status',
      component: 'NSelect',
      label: '状态',
      defaultValue: 0,
      componentProps: {
        options: (function () {
          var arr = [
          {
              label: '全部',
              value: 0,
            },
            {
              label: '已开启',
              value: 1,
            },
            {
              label: '已关闭',
              value: 2,
            },
          ];
          return arr;
        })(),
        onInput: (e: any) => {
          console.log(e);
        },
      },
    },
  ];

  const router = useRouter();
  const message = useMessage();
  const dialog = useDialog();
  const actionRef = ref();
  const optionsRef = ref([]);
  const checkedRowKes = ref<any[]>([]);
  const userStore = useUserStore();
  const isShowOpt = userStore.getRoleType == 'admin' || userStore.getRoleType == 'super'; //是否显示操作按钮
  const formParams = reactive({
    method: 'alipay',
    type: "",
    status: 0
  });
  const actionColumn = reactive({
    width: isShowOpt ? 200 : 150,
    title: '操作',
    key: 'action',
    fixed: 'right',
    render(record) {
      return h(TableAction as any, {
        style: 'button',
        actions: [
          {
            label: '启用',
            onClick: handleOpenPay.bind(null, record),
            type: 'error',
            ifShow: () => {
              return record.status != 1 && isShowOpt;
            },
          },
        ],
        select: (key) => {
          message.info(`您点击了，${key} 按钮`);
        },
      });
    },
  });

  function onCheckedRow(rowKeys: any[]) {
    checkedRowKes.value = rowKeys;
    console.log('checkedRowKes=', checkedRowKes.value);
  }

  async function handleOpenPay(record: Recordable) {
    let pay_method_str = "";
    if(record.method == 'wechat')
    {
      pay_method_str = "微信";
    }
    else if(record.method == 'alipay')
    {
      pay_method_str = "支付宝";
    }
   
    dialog.info({
      title: '提示',
      content: `您确定开启 ${record.desc} ${pay_method_str} ${record.type}`,
      positiveText: '确定',
      negativeText: '取消',
      onPositiveClick: async () => {
        record.status = 'true';
        await changeStatus({ id: record.id, status: 1});
        message.success('启用成功');
        reloadTable();
      },
      onNegativeClick: () => {},
    });
   
  }
  const [register, {}] = useForm({
    gridProps: { cols: '1 s:1 m:2 l:3 xl:5 2xl:4' },
    labelWidth: 76,
    schemas,
  });

  const loadDataTable = async (res) => {
    const param = { ...formParams, ...res };
    console.log("param===", param);
    delete param.makeDate;
    const data = await getPayConfigList(param);
    return data;
  };

  function reloadTable() {
    actionRef.value.reload();
  }

  function handleSubmit(values: Recordable) {
    // console.log('submit', values);
    let tempParams = unref(formParams);
    console.log('tempParams', tempParams);
    tempParams = Object.assign(tempParams, values);
    if (values.makeDate) {
      let [startTime, endTime]: [number, number] = values.makeDate;
      let startTimeS = formatToDate(new Date(startTime));
      let endTimeS = formatToDate(new Date(endTime));
      tempParams = Object.assign(tempParams, { startTime: startTimeS, endTime: endTimeS });
    }
    reloadTable();
  }

  function handleReset(values: Recordable) {
    formParams.status = 0;
    formParams.type = "";
    // console.log('formParams=====', formParams);
    reloadTable();
  }

  onMounted(async () => {
  
  });
</script>
<style lang="less" scoped></style>